import 'package:flutter/material.dart';
import 'package:hrcapp/app_routes.dart';
import 'dart:async';
import '../../utils/theme_colors.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter_svg/flutter_svg.dart';

class AssetMarketScreen extends StatefulWidget {
  const AssetMarketScreen({super.key});

  @override
  _AssetMarketScreenState createState() => _AssetMarketScreenState();
}

class _AssetMarketScreenState extends State<AssetMarketScreen> {
  String _selectedCategory = '全部资产';
  List<Map<String, dynamic>> _assets = [];
  PageController _bannerController = PageController(initialPage: 0);
  int _currentBannerIndex = 0;
  Timer? _bannerTimer;
  final List<Map<String, String>> _bannerImages = [
    {
      'url': 'assets/images/nft/suzhou_embroidery.svg',
      'title': '苏绣艺术',
      'description': '传承千年的刺绣技艺，针针见匠心'
    },
    {
      'url': 'assets/images/nft/guqin_music.svg',
      'title': '古琴音乐', 
      'description': '传统乐器的悠扬旋律，弦弦诉古韵'
    },
    {
      'url': 'assets/images/nft/wood_carving.svg',
      'title': '木雕工艺',
      'description': '匠心独运的雕刻艺术，刀刀显神韵'
    },
    {
      'url': 'assets/images/nft/ceramic_culture.svg',
      'title': '陶瓷文化',
      'description': '千年瓷都的艺术传承，泥火成瑰宝'
    },
  ];



  @override
  void initState() {
    super.initState();
    // 模拟资产数据
    _assets = [
      {
        'name': '苏绣币',
        'description': '苏州传统工艺绣品资产数字化',
        'exchangeRate': '1苏绣币 = 1幅精选苏绣作品',
        'price': '3,500.00',
        'change': '+12.5%',
        'isUp': true,
        'imageUrl': 'assets/images/nft/suzhou_embroidery.svg',
      },
      {
        'name': '古琴币',
        'description': '中国传统民族乐器资产数字化',
        'exchangeRate': '1古琴币 = 1把精品古琴',
        'price': '2,800.00',
        'change': '+8.3%',
        'isUp': true,
        'imageUrl': 'assets/images/nft/guqin_music.svg',
      },
      {
        'name': '木雕币',
        'description': '传统木雕工艺作品资产数字化',
        'exchangeRate': '1木雕币 = 1件精美木雕',
        'price': '1,200.00',
        'change': '-3.1%',
        'isUp': false,
        'imageUrl': 'assets/images/nft/wood_carving.svg',
      },
      {
        'name': '剪纸币',
        'description': '中国传统剪纸艺术资产数字化',
        'exchangeRate': '1剪纸币 = 1套精品剪纸',
        'price': '850.00',
        'change': '+5.2%',
        'isUp': true,
        'imageUrl': 'assets/images/nft/paper_cutting.svg',
      },
      {
        'name': '陶瓷币',
        'description': '景德镇传统陶瓷艺术资产数字化',
        'exchangeRate': '1陶瓷币 = 1件精美陶瓷',
        'price': '1,900.00',
        'change': '-1.7%',
        'isUp': false,
        'imageUrl': 'assets/images/nft/ceramic_culture.svg',
      },
    ];
    
    // 启动自动轮播
    _startBannerTimer();
  }

  // 启动自动轮播计时器
  void _startBannerTimer() {
    _bannerTimer = Timer.periodic(const Duration(seconds: 5), (timer) {
      if (_bannerController.page != null) {
        int nextIndex = (_bannerController.page!.toInt() + 1) % _bannerImages.length;
        _bannerController.animateToPage(
          nextIndex,
          duration: const Duration(milliseconds: 500),
          curve: Curves.easeInOut,
        );
      }
    });
  }

  @override
  void dispose() {
    _bannerTimer?.cancel();
    _bannerController.dispose();
    super.dispose();
  }

  // 构建带错误处理的资产图片组件
  Widget _buildAssetImage(String imageUrl) {
    return SvgPicture.asset(
      imageUrl,
      fit: BoxFit.cover,
      width: double.infinity,
      height: double.infinity,
      placeholderBuilder: (BuildContext context) => Container(
        color: Colors.grey[200],
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Icon(
                Icons.image_outlined,
                size: 32,
                color: Colors.grey[400],
              ),
              const SizedBox(height: 8),
              Text(
                '加载中...',
                style: TextStyle(
                  fontSize: 12,
                  color: Colors.grey[500],
                ),
              ),
            ],
          ),
        ),
      ),
      // 错误处理：当SVG加载失败时显示备用内容
      errorBuilder: (BuildContext context, Object error, StackTrace? stackTrace) {
        print('SVG加载失败: $imageUrl, 错误: $error');
        return Container(
          color: Colors.grey[100],
          child: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Icon(
                  Icons.broken_image_outlined,
                  size: 32,
                  color: Colors.grey[400],
                ),
                const SizedBox(height: 8),
                Text(
                  '图片加载失败',
                  style: TextStyle(
                    fontSize: 12,
                    color: Colors.grey[500],
                  ),
                ),
                const SizedBox(height: 4),
                Text(
                  '点击重试',
                  style: TextStyle(
                    fontSize: 10,
                    color: ThemeColors.primary,
                  ),
                ),
              ],
            ),
          ),
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('非遗资产市场'),
        backgroundColor: ThemeColors.primary,
        elevation: 0,
        centerTitle: true,
        leading: IconButton(
          icon: const Icon(Icons.arrow_back_ios, size: 16, color: Colors.white),
          onPressed: () {
            Navigator.pop(context);
          },
        ),
        actions: [
          IconButton(
            icon: const Icon(Icons.search, size: 16, color: Colors.white),
            onPressed: () {
              // 搜索按钮逻辑
            },
          ),
          IconButton(
            icon: const Icon(Icons.notifications, size: 16, color: Colors.white),
            onPressed: () {
              // 通知按钮逻辑
            },
          ),
          const SizedBox(width: 8),
        ],
      ),
      body: SafeArea(
        child: SingleChildScrollView(
          child: Column(
            children: [
              // Banner轮播图
              _buildBannerCarousel(),
              
              // 平台统计数据
              _buildStatisticsSection(),
              
              // 功能模块按钮
              _buildModuleEntries(),
              
              // 消息公告
              _buildAnnouncementBanner(),
              
              // 搜索栏
              Padding(
                padding: const EdgeInsets.all(16.0),
                child: Container(
                  height: 44,
                  decoration: BoxDecoration(
                    color: const Color(0xFFF5F5F5),
                    borderRadius: BorderRadius.circular(22),
                  ),
                  child: TextField(
                    decoration: InputDecoration(
                      hintText: '搜索非遗资产',
                      prefixIcon: const Icon(Icons.search, size: 16, color: Color(0xFF999999)),
                      border: InputBorder.none,
                      contentPadding: const EdgeInsets.symmetric(vertical: 12),
                    ),
                  ),
                ),
              ),

              // 资产分类标签
              SingleChildScrollView(
                scrollDirection: Axis.horizontal,
                child: Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 16.0),
                  child: Row(
                    children: [
                      _buildCategoryChip('全部资产'),
                      _buildCategoryChip('苏绣'),
                      _buildCategoryChip('古琴'),
                      _buildCategoryChip('木雕'),
                      _buildCategoryChip('陶瓷'),
                      _buildCategoryChip('更多', isMore: true),
                    ],
                  ),
                ),
              ),

              // 资产列表
              Column(
                children: _filteredAssets().map((asset) {
                  return _buildAssetCard(asset, context);
                }).toList(),
              ),
            ],
          ),
        ),
      ),
    );
  }


  // 平台统计数据组件
  Widget _buildStatisticsSection() {
    return Container(
      margin: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
      padding: const EdgeInsets.all(20.0),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(16),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.05),
            blurRadius: 10,
            offset: const Offset(0, 2),
          ),
        ],
      ),
      child: Column(
        children: [
          Row(
            children: [
              Icon(
                Icons.trending_up,
                size: 20,
                color: ThemeColors.primary,
              ),
              const SizedBox(width: 8),
              Text(
                '平台数据',
                style: TextStyle(
                  fontSize: 16,
                  fontWeight: FontWeight.w600,
                  color: ThemeColors.textPrimary,
                ),
              ),
            ],
          ),
          const SizedBox(height: 16),
          Row(
            children: [
              Expanded(
                child: _buildStatCard('1,234', '注册用户', Icons.people),
              ),
              Expanded(
                child: _buildStatCard('567', '非遗作品', Icons.palette),
              ),
              Expanded(
                child: _buildStatCard('89', '传承人', Icons.person_pin),
              ),
              Expanded(
                child: _buildStatCard('¥2.3M', '交易总额', Icons.monetization_on),
              ),
            ],
          ),
        ],
      ),
    );
  }

  // 统计卡片组件
  Widget _buildStatCard(String value, String label, IconData icon) {
    return Column(
      children: [
        Icon(
          icon,
          size: 24,
          color: ThemeColors.primary,
        ),
        const SizedBox(height: 8),
        Text(
          value,
          style: TextStyle(
            fontSize: 18,
            fontWeight: FontWeight.bold,
            color: ThemeColors.textPrimary,
          ),
        ),
        const SizedBox(height: 4),
        Text(
          label,
          style: TextStyle(
            fontSize: 12,
            color: ThemeColors.textSecondary,
          ),
        ),
      ],
    );
  }

  // 功能模块按钮组件
  Widget _buildModuleEntries() {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 16.0),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            '功能服务',
            style: TextStyle(
              fontSize: 18,
              fontWeight: FontWeight.bold,
              color: ThemeColors.textPrimary,
            ),
          ),
          const SizedBox(height: 16),
          Row(
            children: [
              Expanded(
                child: _buildModuleButton('上链', Icons.link, 0, () {
                  Navigator.pushNamed(context, '/digitization');
                }),
              ),
              const SizedBox(width: 8),
              Expanded(
                child: _buildModuleButton('基金', Icons.trending_up, 1, () {
                  Navigator.pushNamed(context, '/foundation');
                }),
              ),
              const SizedBox(width: 8),
              Expanded(
                child: _buildModuleButton('大厅', Icons.account_balance, 2, () {
                  Navigator.pushNamed(context, AppRoutes.heritageExhibition);
                }),
              ),
              const SizedBox(width: 8),
              Expanded(
                child: _buildModuleButton('活动', Icons.event, 3, () {
                  Navigator.pushNamed(context, '/heritage_activities');
                }),
              ),
            ],
          ),
        ],
      ),
    );
  }

  // 单个模块按钮组件
  Widget _buildModuleButton(String title, IconData iconData, int index, [Function()? onTap]) {
    final List<Color> moduleColors = [
      ThemeColors.primary,
      ThemeColors.secondary,
      ThemeColors.accent,
      ThemeColors.warning,
    ];
    
    final color = moduleColors[index % moduleColors.length];
    
    return GestureDetector(
      onTap: onTap,
      child: Container(
        height: 80,
        decoration: BoxDecoration(
          gradient: LinearGradient(
            begin: Alignment.topCenter,
            end: Alignment.bottomCenter,
            colors: [
              color.withOpacity(0.12),
              color.withOpacity(0.06),
            ],
          ),
          borderRadius: BorderRadius.circular(12),
          border: Border.all(
            color: color.withOpacity(0.25),
            width: 1,
          ),
          boxShadow: [
            BoxShadow(
              color: color.withOpacity(0.08),
              blurRadius: 6,
              offset: const Offset(0, 2),
            ),
          ],
        ),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Container(
              width: 32,
              height: 32,
              decoration: BoxDecoration(
                color: color.withOpacity(0.2),
                borderRadius: BorderRadius.circular(8),
              ),
              child: Icon(
                iconData,
                size: 18,
                color: color,
              ),
            ),
            const SizedBox(height: 6),
            Text(
              title,
              style: TextStyle(
                fontSize: 13,
                fontWeight: FontWeight.w600,
                color: ThemeColors.textPrimary,
              ),
            ),
          ],
        ),
      ),
    );
  }



  // Banner轮播图组件
  Widget _buildBannerCarousel() {
    return Container(
      height: 180,
      margin: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(16),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.1),
            blurRadius: 10,
            offset: const Offset(0, 4),
          ),
        ],
      ),
      child: ClipRRect(
        borderRadius: BorderRadius.circular(16),
        child: Stack(
          children: [
            PageView(
              controller: _bannerController,
              children: _bannerImages.map((banner) {
                return _buildBannerItem(
                  imageUrl: banner['url']!,
                  title: banner['title']!,
                  description: banner['description']!,
                );
              }).toList(),
              onPageChanged: (index) {
                setState(() {
                  _currentBannerIndex = index;
                });
              },
            ),
            // 页面指示器
            Positioned(
              bottom: 12,
              left: 0,
              right: 0,
              child: Center(
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: List.generate(
                    _bannerImages.length,
                    (index) => Container(
                      width: 8,
                      height: 8,
                      margin: const EdgeInsets.symmetric(horizontal: 4),
                      decoration: BoxDecoration(
                        shape: BoxShape.circle,
                        color: index == _currentBannerIndex
                            ? ThemeColors.primary
                            : Colors.white.withOpacity(0.5),
                      ),
                    ),
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  // 单个Banner项组件
  Widget _buildBannerItem({
    required String imageUrl,
    required String title,
    required String description,
  }) {
    return Container(
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(16),
      ),
      child: ClipRRect(
        borderRadius: BorderRadius.circular(16),
        child: Stack(
          children: [
            // SVG背景图片
            Positioned.fill(
              child: _buildAssetImage(imageUrl),
            ),
            // 渐变遮罩层
            Container(
              decoration: BoxDecoration(
                gradient: LinearGradient(
                  begin: Alignment.topCenter,
                  end: Alignment.bottomCenter,
                  colors: [
                    Colors.transparent,
                    Colors.black.withOpacity(0.2),
                    Colors.black.withOpacity(0.6),
                  ],
                ),
              ),
            ),
            // 装饰性背景元素
            Positioned(
              top: 20,
              right: 20,
              child: Container(
                width: 60,
                height: 60,
                decoration: BoxDecoration(
                  shape: BoxShape.circle,
                  color: Colors.white.withOpacity(0.1),
                  border: Border.all(
                    color: Colors.white.withOpacity(0.3),
                    width: 1,
                  ),
                ),
                child: Icon(
                  Icons.auto_awesome,
                  color: Colors.white.withOpacity(0.8),
                  size: 24,
                ),
              ),
            ),
            // 主要内容
            Positioned(
              bottom: 0,
              left: 0,
              right: 0,
              child: Container(
                padding: const EdgeInsets.all(20.0),
                decoration: BoxDecoration(
                  borderRadius: const BorderRadius.only(
                    bottomLeft: Radius.circular(16),
                    bottomRight: Radius.circular(16),
                  ),
                  gradient: LinearGradient(
                    begin: Alignment.topCenter,
                    end: Alignment.bottomCenter,
                    colors: [
                      Colors.transparent,
                      Colors.black.withOpacity(0.9),
                    ],
                  ),
                ),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Row(
                      children: [
                        Container(
                          padding: const EdgeInsets.symmetric(
                            horizontal: 8,
                            vertical: 4,
                          ),
                          decoration: BoxDecoration(
                            color: ThemeColors.primary.withOpacity(0.9),
                            borderRadius: BorderRadius.circular(12),
                          ),
                          child: const Text(
                            '非遗精品',
                            style: TextStyle(
                              fontSize: 10,
                              fontWeight: FontWeight.w600,
                              color: Colors.white,
                            ),
                          ),
                        ),
                      ],
                    ),
                    const SizedBox(height: 8),
                    Text(
                      title,
                      style: const TextStyle(
                        fontSize: 20,
                        fontWeight: FontWeight.bold,
                        color: Colors.white,
                        shadows: [
                          Shadow(
                            offset: Offset(0, 1),
                            blurRadius: 3,
                            color: Colors.black54,
                          ),
                        ],
                      ),
                    ),
                    const SizedBox(height: 4),
                    Text(
                      description,
                      style: TextStyle(
                        fontSize: 14,
                        color: Colors.white.withOpacity(0.9),
                        height: 1.3,
                      ),
                    ),
                    const SizedBox(height: 12),
                    Row(
                      children: [
                        Container(
                          padding: const EdgeInsets.symmetric(
                            horizontal: 12,
                            vertical: 6,
                          ),
                          decoration: BoxDecoration(
                            color: Colors.white.withOpacity(0.2),
                            borderRadius: BorderRadius.circular(16),
                            border: Border.all(
                              color: Colors.white.withOpacity(0.3),
                              width: 1,
                            ),
                          ),
                          child: Row(
                            mainAxisSize: MainAxisSize.min,
                            children: [
                              const Text(
                                '了解更多',
                                style: TextStyle(
                                  fontSize: 12,
                                  fontWeight: FontWeight.w500,
                                  color: Colors.white,
                                ),
                              ),
                              const SizedBox(width: 4),
                              Icon(
                                Icons.arrow_forward_ios,
                                size: 10,
                                color: Colors.white.withOpacity(0.8),
                              ),
                            ],
                          ),
                        ),
                      ],
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  // 过滤后的资产列表
  List<Map<String, dynamic>> _filteredAssets() {
    if (_selectedCategory == '全部资产') {
      return _assets;
    }
    return _assets.where((asset) => asset['name']!.contains(_selectedCategory)).toList();
  }

  // 分类标签组件
  Widget _buildCategoryChip(String label, {bool isMore = false}) {
    final isSelected = _selectedCategory == label && !isMore;
    return Padding(
      padding: const EdgeInsets.only(right: 8.0),
      child: ChoiceChip(
        label: Text(label),
        selected: isSelected,
        onSelected: (selected) {
          if (selected && !isMore) {
            setState(() {
              _selectedCategory = label;
            });
          }
        },
        selectedColor: ThemeColors.primary,
        labelStyle: TextStyle(
          color: isSelected ? Colors.white : ThemeColors.textPrimary,
          fontWeight: isSelected ? FontWeight.w500 : FontWeight.normal,
        ),
        backgroundColor: ThemeColors.background,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(16),
        ),
        side: BorderSide(
          color: isSelected ? ThemeColors.primary : ThemeColors.border,
          width: 1,
        ),
      ),
    );
  }

  // 资产卡片组件
  Widget _buildAssetCard(Map<String, dynamic> asset, BuildContext context) {
    return GestureDetector(
      onTap: () {
        // 跳转到资产详情页面
        Navigator.pushNamed(
          context,
          AppRoutes.assetDetail,
          arguments: asset,
        );
      },
      child: Container(
        margin: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(16),
          boxShadow: [
            BoxShadow(
              color: Colors.black.withOpacity(0.08),
              blurRadius: 12,
              offset: const Offset(0, 4),
            ),
          ],
        ),
        child: ClipRRect(
          borderRadius: BorderRadius.circular(16),
          child: Padding(
            padding: const EdgeInsets.all(16.0),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                // 资产图片
                Stack(
                  children: [
                    Container(
                      width: double.infinity,
                      height: 120,
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(12),
                        color: Colors.grey[100],
                      ),
                      child: ClipRRect(
                        borderRadius: BorderRadius.circular(12),
                        child: _buildAssetImage(asset['imageUrl']),
                      ),
                    ),
                    // 热门标签
                    if (asset['isHot'] ?? false)
                      Positioned(
                        top: 8,
                        right: 8,
                        child: Container(
                          padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
                          decoration: BoxDecoration(
                            color: ThemeColors.warning,
                            borderRadius: BorderRadius.circular(10),
                          ),
                          child: const Text(
                            '热门',
                            style: TextStyle(
                              fontSize: 10,
                              color: Colors.white,
                              fontWeight: FontWeight.bold,
                            ),
                          ),
                        ),
                      ),
                  ],
                ),
                const SizedBox(height: 12),
                // 资产信息
                Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Expanded(
                          child: Text(
                            asset['name'],
                            style: TextStyle(
                              fontSize: 16,
                              fontWeight: FontWeight.bold,
                              color: ThemeColors.textPrimary,
                            ),
                          ),
                        ),
                        Text(
                          '¥${asset['price']}',
                          style: TextStyle(
                            fontSize: 16,
                            fontWeight: FontWeight.bold,
                            color: ThemeColors.textPrimary,
                          ),
                        ),
                      ],
                    ),
                    const SizedBox(height: 4),
                    Text(
                      asset['description'],
                      style: TextStyle(
                        fontSize: 12,
                        color: ThemeColors.textSecondary,
                      ),
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                    ),
                    const SizedBox(height: 8),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text(
                          asset['exchangeRate'],
                          style: TextStyle(
                            fontSize: 12,
                            color: ThemeColors.textTertiary,
                          ),
                        ),
                        Container(
                          padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 1),
                          decoration: BoxDecoration(
                            color: asset['isUp']
                                ? ThemeColors.success.withOpacity(0.1)
                                : ThemeColors.error.withOpacity(0.1),
                            borderRadius: BorderRadius.circular(8),
                          ),
                          child: Text(
                            asset['change'],
                            style: TextStyle(
                              fontSize: 12,
                              color: asset['isUp'] ? ThemeColors.success : ThemeColors.error,
                              fontWeight: FontWeight.w500,
                            ),
                          ),
                        ),
                      ],
                    ),
                    const SizedBox(height: 12),
                    SizedBox(
                      width: double.infinity,
                      child: ElevatedButton(
                        onPressed: () {
                          _showExchangeDialog(context, asset);
                        },
                        style: ElevatedButton.styleFrom(
                          backgroundColor: ThemeColors.primary,
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(20),
                          ),
                          padding: const EdgeInsets.symmetric(
                            horizontal: 16,
                            vertical: 10,
                          ),
                          elevation: 0,
                          shadowColor: Colors.transparent,
                        ),
                        child: const Text(
                          '兑换',
                          style: TextStyle(
                            fontSize: 14,
                            fontWeight: FontWeight.w500,
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  // 消息公告横幅组件
  Widget _buildAnnouncementBanner() {
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
      child: GestureDetector(
        onTap: () {
          _showAnnouncementDetail(context);
        },
        child: Container(
          padding: const EdgeInsets.all(12.0),
          decoration: BoxDecoration(
            color: ThemeColors.warning.withOpacity(0.1),
            borderRadius: BorderRadius.circular(12),
            border: Border.all(
              color: ThemeColors.warning.withOpacity(0.3),
              width: 1,
            ),
          ),
          child: Row(
            children: [
              Icon(
                Icons.volume_up,
                size: 18,
                color: ThemeColors.warning,
              ),
              const SizedBox(width: 8),
              Expanded(
                child: Text(
                  '非遗保护基金成立仪式将于6月15日在苏州举行，欢迎报名参加！',
                  style: TextStyle(
                    fontSize: 14,
                    color: ThemeColors.warning,
                  ),
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                ),
              ),
              Icon(
                Icons.chevron_right,
                size: 16,
                color: ThemeColors.warning,
              ),
            ],
          ),
        ),
      ),
    );
  }

  // 显示消息详情对话框
  void _showAnnouncementDetail(BuildContext context) {
    showDialog(
      context: context,
      builder: (context) {
        return Dialog(
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(16),
          ),
          elevation: 4,
          child: Container(
            padding: const EdgeInsets.all(24.0),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                Text(
                  '消息详情',
                  style: const TextStyle(
                    fontSize: 18,
                    fontWeight: FontWeight.bold,
                    color: ThemeColors.textPrimary,
                  ),
                ),
                const SizedBox(height: 20),
                Container(
                  padding: const EdgeInsets.all(16.0),
                  decoration: BoxDecoration(
                    color: ThemeColors.warning.withOpacity(0.1),
                    borderRadius: BorderRadius.circular(12),
                  ),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Row(
                        children: [
                          Icon(
                            Icons.volume_up,
                            size: 18,
                            color: ThemeColors.warning,
                          ),
                          const SizedBox(width: 10),
                          Text(
                            '非遗保护基金成立仪式',
                            style: TextStyle(
                              fontSize: 16,
                              fontWeight: FontWeight.bold,
                              color: ThemeColors.warning,
                            ),
                          ),
                        ],
                      ),
                      const SizedBox(height: 16),
                      Text(
                        '尊敬的用户：\n\n非遗保护基金将于2023年6月15日在苏州国际会议中心举行成立仪式，诚邀您参加。本次活动将邀请多位非遗大师现场展示传统技艺，同时介绍基金运作模式及未来规划。\n\n活动时间：2023年6月15日 14:00-17:00\n活动地点：苏州国际会议中心\n\n请通过下方按钮报名参加，名额有限，先到先得！',
                        style: TextStyle(
                          fontSize: 14,
                          color: ThemeColors.textSecondary,
                        ),
                      ),
                    ],
                  ),
                ),
                const SizedBox(height: 24),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    TextButton(
                      onPressed: () {
                        Navigator.pop(context);
                      },
                      style: TextButton.styleFrom(
                        foregroundColor: ThemeColors.textSecondary,
                      ),
                      child: const Text('取消'),
                    ),
                    ElevatedButton(
                      onPressed: () {
                        // 报名逻辑
                        Navigator.pop(context);
                        ScaffoldMessenger.of(context).showSnackBar(
                          SnackBar(
                            content: const Text('报名成功'),
                            backgroundColor: ThemeColors.success,
                          ),
                        );
                      },
                      style: ElevatedButton.styleFrom(
                        backgroundColor: ThemeColors.primary,
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(20),
                        ),
                        padding: const EdgeInsets.symmetric(
                          horizontal: 24,
                          vertical: 10,
                        ),
                      ),
                      child: const Text('立即报名'),
                    ),
                  ],
                ),
              ],
            ),
          ),
        );
      },
    );
  }

  // 兑换对话框
  void _showExchangeDialog(BuildContext context, Map<String, dynamic> asset) {
    showDialog(
      context: context,
      builder: (context) {
        return Dialog(
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(16),
          ),
          elevation: 4,
          child: Container(
            padding: const EdgeInsets.all(24.0),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                Text(
                  '兑换 ${asset['name']}',
                  style: const TextStyle(
                    fontSize: 18,
                    fontWeight: FontWeight.bold,
                    color: ThemeColors.textPrimary,
                  ),
                ),
                const SizedBox(height: 24),
                Container(
                  width: 100,
                  height: 100,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(16),
                    image: DecorationImage(
                      image: NetworkImage(asset['imageUrl']),
                      fit: BoxFit.cover,
                    ),
                  ),
                ),
                const SizedBox(height: 16),
                Text(
                  '1 ${asset['name']} = ${asset['exchangeRate'].split('=')[1]}',
                  style: TextStyle(
                    fontSize: 14,
                    color: ThemeColors.textSecondary,
                  ),
                ),
                const SizedBox(height: 16),
                Text(
                  '当前价格: ¥${asset['price']}',
                  style: const TextStyle(
                    fontSize: 16,
                    fontWeight: FontWeight.bold,
                    color: ThemeColors.textPrimary,
                  ),
                ),
                const SizedBox(height: 16),
                TextField(
                  keyboardType: TextInputType.number,
                  decoration: InputDecoration(
                    labelText: '兑换数量',
                    border: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(12),
                    ),
                  ),
                ),
                const SizedBox(height: 24),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    TextButton(
                      onPressed: () {
                        Navigator.pop(context);
                      },
                      style: TextButton.styleFrom(
                        foregroundColor: ThemeColors.textSecondary,
                      ),
                      child: const Text('取消'),
                    ),
                    ElevatedButton(
                      onPressed: () {
                        // 兑换逻辑
                        Navigator.pop(context);
                        ScaffoldMessenger.of(context).showSnackBar(
                          SnackBar(
                            content: Text('${asset['name']} 兑换成功'),
                            backgroundColor: ThemeColors.success,
                          ),
                        );
                      },
                      style: ElevatedButton.styleFrom(
                        backgroundColor: ThemeColors.primary,
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(20),
                        ),
                        padding: const EdgeInsets.symmetric(
                          horizontal: 24,
                          vertical: 10,
                        ),
                      ),
                      child: const Text('确认兑换'),
                    ),
                  ],
                ),
              ],
            ),
          ),
        );
      },
    );
  }
}